package edu.nctu.csie.jichang.database.model.cell;


public class ColumnInfoValue {
	
	private ColumnInfo columnInfo = null;
	private Object value;
	
	public ColumnInfoValue(ColumnInfo pColumnInfo,Object pValue){
		columnInfo = pColumnInfo;
		value = pValue;
	}
	public ColumnInfoValue(String pColumnName, ColumnType pType, Object pValue) {
		columnInfo = new ColumnInfo(pColumnName,pType);
		value = pValue;
	}
	public ColumnInfo getColumnInfo() {
		return columnInfo;
	}
	public void setColumnInfo(ColumnInfo columnInfo) {
		this.columnInfo = columnInfo;
	}
	public Object getValue() {
		return value;
	}
	public void setValue(Object value) {
		this.value = value;
	}
	public String toString() {
		return columnInfo.getName()+"="+value.toString();
	}
	public String toSQL(){
		if(value == null){
			return "null";
		}
		switch (columnInfo.getColumnType()) {
			case String:
				return "'"+((String)value).replaceAll("'", "''")+"'";
			case Integer:
				return value.toString();
			case Boolean:
					if (value instanceof Boolean) {
						return ((Boolean) value)?"1":"0";
					}
				return value.toString();
			case Date:
			case Binary:
			default:
				throw new RuntimeException("unknow type " + columnInfo.getColumnType());
		}
	}
}
